home *** CD-ROM | disk | FTP | other *** search
/ The Arsenal Files 6 / The Arsenal Files 6 (Arsenal Computer).ISO / os2 / prjmg104.zip / TDDEMO.C < prev    next >
Text File  |  1995-02-13  |  3KB  |  98 lines

  1. /*
  2. ##################################
  3. #            TDDemo.c:           #
  4. # Thread Demo Program for PrjMgr #
  5. ##################################
  6. # Written 1995 by Roland Knospe  #
  7. ##################################
  8. */
  9.  
  10. /* Need only DOSPROCESS from os2.h */
  11. #define INCL_DOSPROCESS
  12. #include <os2.h>
  13.  
  14. /* Header for the Video library */
  15. #include <sys/video.h>
  16.  
  17. /* Header for _beginthread & _endthread */
  18. #include <stdlib.h>
  19.  
  20. /* Own Header */
  21. #include "TDDemo.h"
  22.  
  23. LONG ThreadCount=0;  /* Counter for active Thread's */
  24.  
  25.  
  26. /* Main Function */
  27. int main( void )
  28. {
  29.    PARAM Param[10]; /* Parameter structures */
  30.    int i;           /* Universal Counter */
  31.  
  32.    v_init();                     /* Init Video Manager */
  33.    v_attrib(B_WHITE|F_BLACK);    /* Set Colors */
  34.    v_clear();                    /* Clear Screen */
  35.    v_gotoxy(13,3);               /* Goto Screen position x=13 y=3 */
  36.    v_printf("Thread Demo: Do not terminate this program !!!!");
  37.  
  38.    /* Start 10 Thread's */
  39.    for(i=0;i<10;i++)
  40.    {
  41.       /* Init Parameter */
  42.       Param[i].Number=i;
  43.       Param[i].MaxCount=100+i*200;
  44.       Param[i].x=10+40*(i%2);
  45.       Param[i].y=7 +2 *(i/2);
  46.       /* Start Thread MyThread with 10k Stacksize */
  47.       _beginthread( MyThread, NULL, 10240, (void*)&Param[i] );
  48.    }
  49.  
  50.    /* Show number of Thread's every 200 msec */
  51.    do
  52.    {
  53.       DosSleep(200);
  54.  
  55.       DosEnterCritSec();
  56.          i=ThreadCount;
  57.          v_gotoxy( 30,20);
  58.          v_printf("Active Threads:%d      ",i);
  59.       DosExitCritSec();
  60.    }
  61.    while ( i>0 ); /* One thread running ?  */
  62.  
  63.    /* Bye Bye ... */
  64.    return 0;
  65. }
  66.  
  67.  
  68. /* Thread Function */
  69. void MyThread( void *arg )
  70. {
  71.    int counter;    /* Counter */
  72.    PARAM *Param;   /* Parameter Pointer */
  73.  
  74.    Param=(PARAM*)arg;   /* Convert *arg to *Param */
  75.  
  76.    DosEnterCritSec();   /* Critical Area !! Only one can Change it at the same time*/
  77.      ThreadCount++;     /* Increase ThreadCounter */
  78.    DosExitCritSec();
  79.  
  80.    DosSleep(100);       /* Wait 100 msec */
  81.  
  82.    /* Counter Loop -> Count from MaxCount to Zero */
  83.    for( counter=Param->MaxCount;counter>=0;counter--)
  84.    {
  85.       DosSleep(10);          /* Wait a moment */
  86.       DosEnterCritSec();     /* Critical Area -> Video library */
  87.          v_gotoxy( Param->x,Param->y);
  88.          v_printf("Counter #%d =%d            ",Param->Number,counter);
  89.       DosExitCritSec();
  90.    }
  91.  
  92.    DosEnterCritSec(); /* Bye Bye .... */
  93.       ThreadCount--;
  94.    DosExitCritSec();
  95.    _endthread();
  96. }
  97.  
  98.